var express = require('express');
var router = express.Router();
var {customQuery} = require('../mysql/index')

// Add a new learn_type
router.post('/add', function(req, res, next) {
    const { name } = req.body;
    console.log('name',name)
    const query = `INSERT INTO learn_type (name) VALUES ('${name}')`;
    console.log('query',query)
    customQuery(query)
        .then(function(result) {
            console.log('result', result)
            return res.status(200).send({ code: 200, msg: '成功添加学习类型' });
        })
        .catch(function(err) {
            console.error(err);
            return res.status(500).send({ code: 500, msg: '内部服务器错误' });
        });
});

// Delete a learn_type
router.post('/delete', function(req, res, next) {
    console.log('进入删除接口')
    const { id } = req.body;
    console.log(id)
    const query = `DELETE FROM learn_type WHERE id = ${id}`;
    customQuery(query)
        .then(function(result) {
            return res.status(200).send({ code: 200, msg: '成功删除学习类型' });
        })
        .catch(function(err) {
            console.error(err);
            return res.status(500).send({ code: 500, msg: '内部服务器错误' });
        });
});

// Get all learn_types
router.get('/', function(req, res, next) {
    const query = 'SELECT * FROM learn_type';
    customQuery(query)
        .then(function(result) {
            return res.status(200).send({ code: 200, list: result });
        })
        .catch(function(err) {
            console.error(err);
            return res.status(500).send({ code: 500, msg: '内部服务器错误' });
        });
});

module.exports = router;
